const db = wx.cloud.database({
  env: 'cloud1-2g6vpgs796710242'
})
const app = getApp()
const _ = db.command

Page({
  data: {
    conference_list:[],
    list:[],
    Switchlist:[],
    listlength:"",
    sorttype:0,
    planet_info:[],
    storage:[]
  },

  onLoad: function (options) {
    var user = wx.getStorage({
      key:'user',
      success: (res)=> {
        console.log("查询用户信息缓存",res.data)
        this.setData({
          storage:res.data
        })
      }
    })
    var id = options.id
    this.gethistory()
    db.collection('cp_list')
    .doc(id)
    .get()
    .then(res=>{
      this.setData({
        planet_info:res.data
      })
      //CTR自增
      db.collection("cp_list")
      .doc(id)
      .update({
        data:{
          CTR:_.inc(1)
        }
      })
      .then(res=>{
        console.log("CTR自增成功",res)
      })
      if (!this.data.storage.name=="") {
        console.log('用户信息已授权',user)
        this.setData({
          hasUserInfo: true,
          userInfo: user,
        })
        console.log("用户信息", this.data.userInfo)
        console.log("全局openid",app.globalData.openid)

        db.collection("User")
        .where({
          _openid:app.globalData.openid
        })
        .get()
        .then(res=>{
          console.log("用户信息",res)
          var aimcp = this.data.planet_info.title
          for (var i = 0; i < res.data[0].common.length; i++) {
            if (res.data[0].common[i].name==aimcp) {
              db.collection("User")
              .where({
                _openid:app.globalData.openid,
                'common.name':aimcp
              })
              .update({
                data:{
                  "common.$.count":res.data[0].common[i].count+1
                }
              })
              .then(res=>{
                console.log("使用频次更新成功",res)
              })
              .catch(err=>{
                console.log(err)
              })
            }
          }
        })
      } else {
        console.log("无缓存用户信息")
      }
    })
    .catch(err=>{
      console.log(err)
    })
  },

  gethistory() {
    var compare = function (prop) {
      return function (obj1, obj2) {
          var val1 = obj1[prop];
          var val2 = obj2[prop];if (val1 < val2) {
              return -1;
          } else if (val1 > val2) {
              return 1;
          } else {
              return 0;
          }            
      } 
    }
    wx.showLoading({
      title: '加载中',
    })
    let len = this.data.list.length
    console.log("当前list长度",len)
    wx.cloud.callFunction({
      name:'sy_up',
      data: {
        len:len,
        pageNum:10,
      }
    }).then(res=>{
      wx.hideLoading()
      console.log('调用成功',res)
      let datalist = res.result.data
      if (datalist.length <=0) {
        wx.showToast({
          icon:'none',
          title: '没有更多数据了',
        })
      }
      this.setData({
        list: this.data.list.concat(datalist).sort(compare("read")).reverse(),
        Switchlist: this.data.list.concat(datalist).sort(compare("time")).reverse(),
        listlength: datalist.length
      })
      console.log("显示",this.data.list)
    }).catch(err=>{
      wx.hideLoading()
      console.log('调用失败',err)
    })
  },

  go_detail(e) {
    console.log('conference-id',e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '/pages/sw_detail/sw_detail?id=' + e.currentTarget.dataset.id,
    })
  },

  switch_sort() {
    // hottest=0,latest=1
    if (this.data.sorttype==1) {
      this.setData({
        sorttype:0
      })
    } else {
      this.setData({
        sorttype:1
      })
    }
    console.log("当前状态",this.data.sorttype)
  },

  add_conference() {
    if (app.globalData.ulevel<3) {
      wx.showToast({ title: '您账号目前等级还未到3级，不具备发布项目权限', icon: 'none',duration:3000 });
    } else {
      //'edit'表示编辑已有项目，'creator'表示新增项目
      wx.navigateTo({
        url: '../release/release?channel=' + "creator"
      })
    }
  },

  onShareAppMessage: function () {

  }
})